package com.ruoyi.rei.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.rei.domain.WorkflowRei;
import com.ruoyi.rei.mapper.WorkflowReiMapper;
import com.ruoyi.rei.service.IWorkflowReiService;
import com.ruoyi.system.service.ISysUserService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 报销Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-13
 */
@Service
public class WorkflowReiServiceImpl implements IWorkflowReiService
{
    @Autowired
    private WorkflowReiMapper workflowReiMapper;

    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private TaskService taskService;

    /**
     * 查询报销
     *
     * @param id 报销ID
     * @return 报销
     */
    @Override
    public WorkflowRei selectWorkflowReiById(String id)
    {
        return workflowReiMapper.selectWorkflowReiById(id);
    }

    /**
     * 查询报销列表
     *
     * @param workflowRei 报销
     * @return 报销
     */
    @Override
    public List<WorkflowRei> selectWorkflowReiList(WorkflowRei workflowRei)
    {
        return workflowReiMapper.selectWorkflowReiList(workflowRei);
    }

    /**
     * 新增报销
     *
     * @param workflowRei 报销
     * @return 结果
     */
    @Override
    public int insertWorkflowRei(WorkflowRei workflowRei)
    {
        String id = UUID.randomUUID().toString();
        workflowRei.setId(id);
        workflowRei.setCreateTime(DateUtils.getNowDate());
        String join = StringUtils.join(sysUserService.selectUserNameByPostCodeAndDeptId("se", SecurityUtils.getLoginUser().getUser().getDeptId()), ",");
        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("reiApprove")
                .withName(workflowRei.getTitle())
                .withBusinessKey(id)
                .withVariable("deptfzr",join)
                .build());
        workflowRei.setInstanceId(processInstance.getId());
        workflowRei.setState("0");
        workflowRei.setCreateName(SecurityUtils.getNickName());
        workflowRei.setCreateBy(SecurityUtils.getUsername());
        workflowRei.setCreateTime(DateUtils.getNowDate());
        workflowRei.setCreateTime(DateUtils.getNowDate());
        return workflowReiMapper.insertWorkflowRei(workflowRei);
    }

    /**
     * 修改报销
     *
     * @param workflowRei 报销
     * @return 结果
     */
    @Override
    public int updateWorkflowRei(WorkflowRei workflowRei)
    {
        workflowRei.setUpdateTime(DateUtils.getNowDate());
        return workflowReiMapper.updateWorkflowRei(workflowRei);
    }

    /**
     * 批量删除报销
     *
     * @param ids 需要删除的报销ID
     * @return 结果
     */
    @Override
    public int deleteWorkflowReiByIds(String[] ids)
    {
        return workflowReiMapper.deleteWorkflowReiByIds(ids);
    }

    /**
     * 删除报销信息
     *
     * @param id 报销ID
     * @return 结果
     */
    @Override
    public int deleteWorkflowReiById(String id)
    {
        return workflowReiMapper.deleteWorkflowReiById(id);
    }
}
